// pages/buycar/buycar.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: "",
    add: "",
    goodsInfo:null,
    imgsrv: app.globalData.srvlink + '/images/',
    lists: '',
    wid: ''
  },
  onLoad: function(s) {
    var icon = app.globalData.joinCar
    //debugger;

    //console.log(icon)

    if (icon.length == 0) {
      this.setData({
        imgUrl: "../../images/cart1.png",
        add: "去添加点什么吧",
        wid: '0',
        lists: ''
      });
    } else if (icon.length != 0) {
      this.setData({
        imgUrl: "",
        add: "",
        lists: icon,
        goodsInfo:icon[0],
        wid: "100%",
        hei: "100rpx",
      })

       
    }
  },

  wxpay: function() {
    var that = this
    var allprice = this.data.goodsInfo.price;
    var userData = wx.getStorageSync("userInfo")
    var openId = wx.getStorageSync("openId")

    wx.showModal({
      title: '提示',
      content: '支付金额为：' + allprice + '元,确定要支付吗？',
      success: function(resconf) {
        if (resconf.confirm) {
          var code = null;
          var orderData = {};
          orderData.amount = allprice;
          orderData.qty = 1
          orderData.userId = openId;
          orderData.userName = 'test'; //userData.nickName;
          orderData.tableNO ="222";// app.globalData.tableNo;
          orderData.tableName ="test";// app.globalData.tableNo;
          orderData.goodsId = that.data.goodsInfo.goodsId
          orderData.goodsName = that.data.goodsInfo.goodsName
          orderData.imgUrl = that.data.goodsInfo.img


          wx.request({
            url: app.globalData.srvlink + "/order",
            data: orderData,
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              //ok
              if (res.data.code == '1') {
                console.log(res.data.data);
                //发起支付                
                //that.xiadan(res.data.data);
              }

            }
          });


        } else {
          console.log('用户点击取消')

        }

      }
    })
  },

  deletePay: function(e) {
    wx.showModal({
      title: '删除提示',
      content: '您确定要删除吗？',
      success: function(res) {
        if (res.confirm) {
          app.globalData.joinCar.splice(e.currentTarget.id, 1);
          var page = getCurrentPages().pop();
          page.onLoad();
        } else {
          console.log("用户点击取消")
        }
      }
    })

  },
  onShow: function() {
    this.onLoad();
  },

})